package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.PlayActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */

public class BannerAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> imgBannerList;

    public BannerAdapter(Context context, List<String> imgBannerList) {
        this.mContext = context;
        this.imgBannerList = imgBannerList;
        this.mInflater = LayoutInflater.from(context);
    }

    public void replaceAll(List<String> bannerList) {
        if (bannerList == null)
            return;
        if (imgBannerList == null)
            imgBannerList = new ArrayList<String>(bannerList);
        else {
            imgBannerList.clear();
            imgBannerList.addAll(bannerList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return imgBannerList != null ? Integer.MAX_VALUE : 1;
    }

    /**
     * 实际数据Size
     *
     * @return
     */
    public int size() {
        return imgBannerList != null ? imgBannerList.size() : 1;
    }

    @Override
    public String getItem(int arg0) {
        return imgBannerList != null ? imgBannerList.get(arg0 % size()) : null;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        try {
            if (arg1 == null) {
                arg1 = mInflater.inflate(R.layout.adapter_image_item, arg2, false);
            }
            ImageView imageView = (ImageView) arg1.findViewById(R.id.ivImg);
            if (imgBannerList == null || imgBannerList.size() == 0) {
                imageView.setImageResource(R.mipmap.iv_loading);
            } else {
                final String mVpInfo = getItem(arg0);
                String url=mVpInfo.endsWith(".mp4")?Url.IMG_URL + mVpInfo+"?vframe/jpg/offset/10/w/640/h/360":Url.IMG_URL + mVpInfo;
                Glide.with(mContext).load(url).placeholder(R.mipmap.iv_loading).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                if (mVpInfo.endsWith(".mp4")){
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(mContext, PlayActivity.class);
                            intent.putExtra("url",Url.IMG_URL + mVpInfo);
                            mContext.startActivity(intent);
                        }
                    });
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return arg1;
    }

}
